﻿using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.CodeRule;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.CodeRule;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendNet.Service
{
    public class CodeRuleService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public CodeRuleService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取编码规则列表
         */
        public async Task<RequestResponseModel<PageResult<CodeRuleView>>> GetCodeRuleList(CodeRuleSearchDTO codeRuleSearchDTO)
        {
            //筛选
            var query = _context.CodeRuleView
                .WhereIF(a => a.Code.Contains(codeRuleSearchDTO.Code), !string.IsNullOrEmpty(codeRuleSearchDTO.Code))
                .WhereIF(a => a.Rule.Contains(codeRuleSearchDTO.Rule), !string.IsNullOrEmpty(codeRuleSearchDTO.Rule))
                ;

            //返回列表
            var codeRuleList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(codeRuleSearchDTO.CurrentPage, codeRuleSearchDTO.PageSize)
                .ToListAsync();
            //计算总数
            var pageCount = await query.CountAsync();

            var pageResult = new PageResult<CodeRuleView>(codeRuleList, pageCount);

            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增编码规则
         */
        public async Task<RequestResponseModel<string>> AddCodeRule(CodeRuleAddDTO codeRuleAddDTO)
        {
            //查找数据
            var query = await _context.CodeRuleView.SingleOrDefaultAsync(a => a.Id == "b2f6d7d0-cf63-4b1e-a717-0d3d27e1a45e");

            if (query == null)
            {
                return RequestResponse.CreateResponse(500, "源数据已被清除，请联系管理员", "");
            }

            //操作数据
            query.Number = query.Number + 1;
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var codeRule = new CodeRuleModel
            {
                Id = guid.ToString(),
                Code = query.Rule + query.Number.ToString(),
                Rule = codeRuleAddDTO.Rule,
                Number = 0,
                CreateTime = date
            };

            //更新编码规则表
            _context.CodeRule.Update(codeRule);
            //新增编码规则
            _context.CodeRule.Add(codeRule);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 更新编码规则
         */
        public async Task<RequestResponseModel<string>> PutCodeRule(CodeRuleUpdateDTO codeRuleUpdateDTO)
        {
            //查找数据
            var query = await _context.CodeRule.FindAsync(codeRuleUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", codeRuleUpdateDTO.Id);
            };

            query.Rule = codeRuleUpdateDTO.Rule;

            //更新编码规则表
            _context.CodeRule.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", codeRuleUpdateDTO.Id);
        }

        /**
         * 删除编码规则
         */
        public async Task<RequestResponseModel<string>> DeleteCodeRule(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", "");
            }

            //校验id有效性
            var query = await _context.CodeRule.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //删除操作
            _context.CodeRule.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 批量删除
         */
        public async Task<RequestResponseModel<List<string>>> DeleteCodeRuleMulti(List<string> Ids)
        {
            //判空
            if (Ids == null || Ids.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids为空", new List<string>());
            }

            //查找id
            var query = await _context.CodeRule
                .Where(a => Ids.Contains(a.Id))
                .ToListAsync();

            //是否有匹配资源
            if (query.Count == 0)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //操作数据库
            _context.CodeRule.RemoveRange(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }

        /**
         * 获取编码
         */
        public async Task<RequestResponseModel<string>> GetNewCode(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", "");
            }

            //校验Id有效性
            var query = await _context.CodeRule.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //生成编码
            query.Number++;

            //存库
            _context.CodeRule.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "获取成功", query.Rule + query.Number);
        }
    }
}
